const Controller = require('../prototype/Controller')
const { models } = require('../model')

class Topic extends Controller {
  constructor () {
    // 查询条件过滤器
    const queryFilter = (query) => {
      const findQuery = {
        ...query
      }
      if (query.keywords || query.keywords === '') {
        findQuery.$or = [
          { name: { $regex: `${query.keywords}` } },
          { description: { $regex: `${query.keywords}` } }
        ]
        delete findQuery.keywords
      }
      return findQuery
    }
    super({ model: models.Topic, queryFilter })
  }

  findAll () {
    return super.findAll()
  }

  updateStatus () {
    return async (ctx) => {
      const { id, status } = ctx.request.body
      const result = await this.model.updateById(id, {
        $set: { status: status }
      })
      ctx.wrapper.succ(result)
    }
  }
}

module.exports = Topic
